var DBFactory = require('../db/mysql_db');
var reportDao = module.exports;

reportDao.list = function(data, cb) {
    var sql = 'select * from report where 1 ';
    var sqlTotal = 'select count(*) as count from report where 1 ';
    var arr = [];
    if(data.status) {
        sql += ' and status = ? ';
        sqlTotal += ' and status = ? ';
        arr.push(data.status);
    }
    sql += ' order by created_date desc ';
    DBFactory.executeSql(sqlTotal, arr, 'reportListTotal', function(err, total) {
        if(err) return cb(err);
        if(total[0].count == 0) return cb(null, {total:0});
        sql += ' limit ? , ? ';
        arr.push(data.start);
        arr.push(data.size);
        DBFactory.executeSql(sql, arr, 'reportList', function(e, result) {
            if(e) return cb(e);
            cb(null, {total: total[0].count, data: result});
        })
    })
}

reportDao.report = function(data, cb) {
    if(!data) return cb(new ERR(500));
    var feedInfo = {};
    ['openid', 'avatar', 'nick', 'reason'].forEach(function(c) {
        if(data[c]) feedInfo[c] = data[c];
    })
    var sql = 'insert into report set ? ';
    DBFactory.executeSql(sql, feedInfo, 'feed', function(e) {
        if(e) return cb(e);
        cb(null, '反馈成功！');
    })
}

reportDao.updateStatus = function(data, cb) {
    if(!data) return cb(new ERR(500));
    var sql = 'update  report set status = ? where id = ? ';
    DBFactory.executeSql(sql, [data.status, data.id], 'updateStatus', function(e) {
        if(e) return cb(e);
        cb(null, '成功！');
    })
}